package com.crazymo.binderpool;

import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private final static String TAG="MoBinderPool";
    BinderPool binderPool;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    private void doWork(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                IBinder secBinder= binderPool.queryBinder(BinderPool.SECURITY_CODE);
                ISecurityCenter securityCenter=SecurityCenterImpl.asInterface(secBinder);

                try {
                    String encryptStr=securityCenter.encrypt("hello crazymo");
                    Log.d(TAG,"BinderPool#encode="+encryptStr);
                    String decryptStr=securityCenter.decrypt(encryptStr);
                    Log.d(TAG,"BinderPool#decode="+decryptStr);
                    IBinder computeBinder = binderPool.queryBinder(BinderPool.COMPUTE_CODE);
                    ICompute compute= ComputeImpl.asInterface(computeBinder);
                    Log.d(TAG,"BinderPool#add="+compute.add(222,666));

                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

    public void useBinderPool(View view) {
        doWork();
    }

    public void initBinderPool(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                binderPool=BinderPool.getInstance(MainActivity.this);
            }
        }).start();
    }
}